﻿using System.Data;
using System.Linq;
using System.Reflection;
using System.Collections.Generic;
using Dapper;
using DapperExtensions;
using DapperExtensions.Mapper;
using DapperExtensions.Sql;
using MySql.Data.MySqlClient;
using Microsoft.Extensions.Configuration;

using Read.Models;
using Microsoft.Extensions.Options;

namespace Read.Services
{
    public class BaseService
    {
        protected IDatabase db;
        protected static string connString = GlobalService.Configuration.GetConnectionString("MySqlConnection");
        //protected static string connString = "server=192.168.16.112;database=reads;uid=root;pwd=root;charset='utf8'";
        protected IDbConnection connection;

        /// <summary>
        /// 构造函数
        /// </summary>
        public BaseService()
        {
            using (connection = new MySqlConnection(connString))
            {
                var config = new DapperExtensionsConfiguration(typeof(AutoClassMapper<>), new List<Assembly>(), new MySqlDialect());
                var sqlGenerator = new SqlGeneratorImpl(config);
                db = new Database(connection, sqlGenerator);
            }
        }

        /// <summary>
        /// 关闭链接
        /// </summary>
        public void CloseConnection()
        {
            if (connection == null) return;
            connection.Close();
            connection.Dispose();
            connection = null;
        }
    }
}
